﻿using System;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.IO;

using Microsoft.SharePoint;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;

public partial class ws_Catalog_Users : System.Web.UI.Page
{

    #region Members
    string ConnStr = GetAppSetting("BIConnect");
    string strYear, strMonth;
    #endregion

    #region Common
    static public string GetAppSetting(String mName)
    {
        String vStr = null;
        string strPath =
    @"C:\inetpub\wwwroot\wss\VirtualDirectories\80\web.config";
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(strPath);
        XmlNode node = xmlDoc.SelectSingleNode(@"/configuration/appSettings");
        foreach (XmlNode keynode in node.ChildNodes)
        {
            string keyStr = keynode.Attributes["key"].Value;
            if (keyStr == mName)
            {
                vStr = keynode.Attributes["value"].Value;
                break;
            }
        }

        if (String.IsNullOrEmpty(vStr))
            vStr = "";

        return vStr;
    }
    #endregion

    #region Page Events

    protected override void OnPreInit(EventArgs e)
    {
        base.OnPreInit(e);
        // Enabled trace in code
        this.Context.Trace.IsEnabled = false;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Params["Year"] != null)
            strYear = Request.Params["Year"].ToString();
        else
            strYear = DateTime.Now.Year.ToString();

        if (Request.Params["Month"] != null)
            strMonth = Request.Params["Month"].ToString();
        else
            strMonth = DateTime.Now.Month.ToString();

        XmlDocument xReturn = getXML();
        Response.ContentType = "text/xml";
        Response.Write(xReturn.OuterXml);
    }

    #endregion

    #region Data

    public XmlDocument getXML()
    {
        int Year = Convert.ToInt32(strYear);
        int Month = Convert.ToInt32(strMonth);

        XmlDocument xReturn = new XmlDocument();
        DataTable tbl = new DataTable("tbl");

        Exception err = new Ko2.Portal.Users.SPUsersHelper().GetUsers(ref tbl, @"http://localhost/");

        #region Make Table

        if (err != null)
        {
            tbl = new DataTable();

            tbl.Columns.Add("ID", typeof(string));
            tbl.Columns[0].Caption = "ID";
            tbl.Columns.Add("Name", typeof(string));
            tbl.Columns[1].Caption = "Name";

            DataRow r = tbl.NewRow();
            r[0] = "-1000";
            r[1] = "Ошибка получения данных.";
            tbl.Rows.Add(r);
        }

        #endregion

        #region XML stream

        using (StringWriter stringWriter = new StringWriter())
        {
            using (XmlTextWriter xmlTextwriter = new XmlTextWriter(stringWriter))
            {
                tbl.WriteXml(xmlTextwriter, XmlWriteMode.WriteSchema);
                xReturn.LoadXml(stringWriter.ToString());
                XmlNode xErrors = xReturn.CreateElement("Errors");
                if (err != null)
                {
                    XmlNode xErr = xReturn.CreateElement("Err");
                    xErr.InnerText = err.Message;
                    xErrors.AppendChild(xErr);
                }
                xReturn.ChildNodes[0].AppendChild(xErrors);
            }
        }

        #endregion

        return xReturn;
    }

    #endregion

}